using System;
using System.Collections;
using System.Collections.Generic;
using ILRuntime.CLR.Method;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;

using UnityEngine;


public class CLRException : CrossBindingAdaptor
{
    public override Type BaseCLRType
    {
        get { return typeof(Exception); }
    }

    public override Type AdaptorType
    {
        get { return typeof(Adaptor); }
    }

    public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
    {
        return new Adaptor(appdomain, instance);
    }

    //实际的适配器类需要继承你想继承的那个类，并且实现CrossBindingAdaptorType接口
    class Adaptor : Exception, CrossBindingAdaptorType
    {
        ILTypeInstance _instance;
        ILRuntime.Runtime.Enviorment.AppDomain _appdomain;
        
      

        //缓存这个数组来避免调用时的GC Alloc
        object[] _param1 = new object[1];
        

        public Adaptor() { }

        public Adaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
        {
            this._appdomain = appdomain;
            this._instance = instance;

       
        }

        public ILTypeInstance ILInstance { get { return _instance; } }    

        public override string ToString()
        {
            IMethod m = _appdomain.ObjectType.GetMethod("ToString", 0);
            m = _instance.Type.GetVirtualMethod(m);
            if (m == null || m is ILMethod)
            {
                return _instance.ToString();
            }
            else return _instance.Type.FullName;
        }
    }
}
